package com.mc.code.web.sys;

import com.mc.code.entity.sys.SysUser;
import com.mc.code.mapper.SysUserMapper;
import com.mc.code.sysService.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 管理员管理
 */

@Controller
public class AdminController {

    //分页
    private static final long serialVersionUID = 1L;
    private static final int PAGE_COUNT = 3;//每页显示记录数

    @Autowired
    private SysUserService sysUserService;

    //对所有管理员列表,分页，多条件查询
    @RequestMapping("/adminManage")
    public String manageAdmin(String adminNo,String adminName,String state, Model model,HttpServletRequest request){
//        System.out.println(1);
//        List<SysUser> adminList = sysUserService.findUserList();
        int  currentPage = 1;
        String page = request.getParameter("page");
        if(page != null){
            currentPage = Integer.valueOf(page);
        }

        //获得查询的信息
        List<SysUser> adminList = sysUserService.findUserByCondition(adminNo,adminName,state,currentPage,PAGE_COUNT);
        //获取总记录数
        int totalPage = sysUserService.getTotalPage(PAGE_COUNT,adminNo,adminName,state);
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("adminNo",adminNo);
        model.addAttribute("adminName",adminName);
        model.addAttribute("state",state);
        model.addAttribute("adminList",adminList);
        return "sys/adminList";
    }


    //跳转到添加管理员的页面
    @RequestMapping("/jumpToAddAdmin")
    public String jumpToAddAdmin(Model model){
        model.addAttribute("adminNo",sysUserService.findMaxId());
        return "sys/addAdmin";
    }


    //添加管理员
    @RequestMapping(value = "/addAdmin",method = {RequestMethod.POST})
    public String addAdmin(HttpServletRequest request,Model model){
        String adminNo = request.getParameter("adminNo");
        String adminName = request.getParameter("adminName");
        String mobile = request.getParameter("mobile");
        String password = new BCryptPasswordEncoder().encode( request.getParameter("password"));
        String state = "1";
        //将数据封装到系统管理员对象中
        SysUser sysUser = new SysUser(adminNo,adminName,password,mobile,state);
        //将数据添加到数据库中
        sysUserService.save(sysUser);
        //根据新加的管理员查询id
        int sysUserId = sysUserService.findIdByAdminNo(adminNo);
        int sysRoleId = Integer.parseInt(request.getParameter("right"));
//        System.out.println("管理员id" + sysUserId + "角色id" + sysRoleId);
        //向sys_user_role表中插入数据
        sysUserService.saveToUserRole(sysUserId,sysRoleId);
        //重定向到管理员列表
        return "redirect:adminManage";
    }

    //启用管理员
    @RequestMapping("/enable")
    public String enable(int id,Model model){
        if (id != 0) {
            int count = sysUserService.enableById(id);
            model.addAttribute("count", count);
        }
        return "redirect:adminManage";
    }

    //禁用管理员
    @RequestMapping("/disable")
    public String disable(int id,Model model){
//        System.out.println(id);
        if (id != 0) {
            int count = sysUserService.disableById(id);
            model.addAttribute("count", count);
        }
        return "redirect:adminManage";
    }

    //返回数据并跳转到修改管理员的页面
    @RequestMapping("/jumpToModifyAdmin")
    public String jumpToModifyAdmin(Model model,SysUser sysUser,int id){
//        System.out.println(id);
        if(id != 0){
            sysUser = sysUserService.findUserById(id);
//            System.out.println(sysUser);
            model.addAttribute("sysUser",sysUser);
        }
        return "sys/modifyAdmin";
    }

    //修改管理员信息并传到数据库中
    @RequestMapping("/updateAdmin")
    public String updateAdmin(SysUser sysUser,HttpServletRequest request){
        int userId = Integer.parseInt(request.getParameter("id"));
        String adminNo = request.getParameter("adminNo");
        String adminName = request.getParameter("adminName");
        String state = request.getParameter("state");
        int roleId = Integer.parseInt(request.getParameter("right"));
        sysUser = new SysUser(userId,adminNo,adminName,state);
        sysUserService.updateUserById(sysUser);
        sysUserService.updateRole(userId,roleId);
        return "redirect:adminManage";
    }

}
